package com.retail.shop.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.retail.shop.entity.vo.RetailShopVo;
import com.retail.shop.entity.vo.ShopCardLogVo;
import com.retail.core.entity.ParamReq;
import com.retail.shop.entity.request.ShopCardLogRequest;
import com.retail.shop.entity.vo.ShopCardVo;
import com.retail.shop.mapper.RetailShopMapper;
import com.retail.shop.mapper.ShopCardLogMapper;
import com.retail.shop.mapper.ShopCardMapper;
import com.retail.shop.service.ShopCardLogService;
import com.retail.team.enums.VerifyFlagEnums;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 开卡记录 Service impl
 *
 * @author Administrator
 */
@Service
public class ShopCardLogServiceImpl implements ShopCardLogService {

    @Resource
    private ShopCardLogMapper shopCardLogMapper;

    @Resource
    private ShopCardMapper shopCardMapper;

    @Resource
    private RetailShopMapper retailShopMapper;

    @Override
    public ShopCardLogVo getModel(Long id) {
        ShopCardLogVo shopCardLogVo = shopCardLogMapper.getModel(id);
        if (ObjectUtil.isNotEmpty(shopCardLogVo) && ObjectUtil.isNotEmpty(shopCardLogVo.getCardType())) {
            ShopCardVo shopCardVo = shopCardMapper.getModel(shopCardLogVo.getCardType());
            if (ObjectUtil.isNotEmpty(shopCardVo)) {
                shopCardLogVo.setCardTypeName(shopCardVo.getCardName());
            }
        }

        if (ObjectUtil.isNotEmpty(shopCardLogVo) && ObjectUtil.isNotEmpty(shopCardLogVo.getShopId())) {
            RetailShopVo shopVo = retailShopMapper.getModel(shopCardLogVo.getShopId());
            shopCardLogVo.setShopName(shopVo.getPoiName());
        }

        shopCardLogVo.setVerifyFlagName(VerifyFlagEnums.getStatusValues(shopCardLogVo.getVerifyFlag()));

        return shopCardLogVo;
    }

    @Transactional
    @Override
    public Long save(ShopCardLogRequest shopCardLogRequest) {
        shopCardLogMapper.save(shopCardLogRequest);
        // 更新数据记录
        Long id = shopCardLogRequest.getShopId();
        saveShopCardLog(shopCardLogRequest, id);

        return shopCardLogRequest.getId();
    }

    private void saveShopCardLog(ShopCardLogRequest shopCardLogRequest, Long id) {
        RetailShopVo retailShopVo = retailShopMapper.getModel(id);
        if (ObjectUtil.isNotEmpty(retailShopVo) && ObjectUtil.equal(shopCardLogRequest.getVerifyFlag(), VerifyFlagEnums.SHEN_HE_TONGGUO.getValue())) {
            if (ObjectUtil.isEmpty(shopCardLogRequest.getCardType())) {
                retailShopVo.setCardType(shopCardLogRequest.getCardType());
                retailShopVo.setCardStartTime(shopCardLogRequest.getStartTime());
                retailShopVo.setCardEndTime(shopCardLogRequest.getEndTime());
                retailShopMapper.updateCardType(retailShopVo);
            } else {
                if (ObjectUtil.equal(retailShopVo.getCardEndTime(), shopCardLogRequest.getStartTime())) {
                    retailShopVo.setCardEndTime(shopCardLogRequest.getEndTime());
                    retailShopMapper.updateCardType(retailShopVo);
                } else {
                    retailShopVo.setCardType(shopCardLogRequest.getCardType());
                    retailShopVo.setCardStartTime(shopCardLogRequest.getStartTime());
                    retailShopVo.setCardEndTime(shopCardLogRequest.getEndTime());
                    retailShopMapper.updateCardType(retailShopVo);
                }
            }
        }
    }

    @Transactional
    @Override
    public Long update(ShopCardLogRequest shopCardLogRequest) {
        shopCardLogMapper.update(shopCardLogRequest);
        Long id = shopCardLogRequest.getShopId();
        saveShopCardLog(shopCardLogRequest, id);
        return shopCardLogRequest.getId();
    }

    @Override
    public List<ShopCardLogVo> listPage(ParamReq<ShopCardLogRequest> params) {
        params.setStartIndex((params.getCurrent() - 1) * params.getSize());
        List<ShopCardLogVo> log =  shopCardLogMapper.listPage(params);
        log.forEach(l ->{
            if (ObjectUtil.isNotEmpty(l) && ObjectUtil.isNotEmpty(l.getCardType())) {
                ShopCardVo shopCardVo = shopCardMapper.getModel(l.getCardType());
                if (ObjectUtil.isNotEmpty(shopCardVo)) {
                    l.setCardTypeName(shopCardVo.getCardName());
                }
            }

            if (ObjectUtil.isNotEmpty(l) && ObjectUtil.isNotEmpty(l.getShopId())) {
                RetailShopVo shopVo = retailShopMapper.getModel(l.getShopId());
                if (ObjectUtil.isNotEmpty(shopVo)) {
                    l.setShopName(shopVo.getPoiName());
                }
            }
            l.setVerifyFlagName(VerifyFlagEnums.getStatusValues(l.getVerifyFlag()));
        });

        return log;
    }

    @Override
    public Integer countPage(ParamReq<ShopCardLogRequest> params) {
        return shopCardLogMapper.countPage(params);
    }

    @Override
    public Integer updateDelFlagById(Long id, Integer delFlag) {
        return shopCardLogMapper.updateDelFlagById(id, delFlag);
    }

    @Override
    public Integer updateShopCard() {
        return retailShopMapper.updateShopCard();
    }
}
